home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 42 / wind12.prf < prev    next >
Text File  |  1986-07-17  |  18KB  |  385 lines

  1. .!****************************************************************************
  2. .! 
  3. .! ANTIC PUBLISHING INC., COPYRIGHT 1985.  REPRINTED BY PERMISSION.
  4. .!
  5. .! ** Professional GEM ** by Tim Oren
  6. .!
  7. .! Proff File by ST enthusiasts at
  8. .! Case Western Reserve University
  9. .! Cleveland, Ohio
  10. .! uucp : decvax!cwruecmp!bammi
  11. .! csnet: bammi@case
  12. .! arpa : bammi%case@csnet-relay
  13. .! compuserve: 71515,155
  14. .!
  15. .!****************************************************************************
  16. .!
  17. .!
  18. .!****************************************************************************
  19. .!
  20. .!            Begin Part 12
  21. .!
  22. .!****************************************************************************
  23. .!
  24. .PART XII GEM Events and Program Structure
  25. .PP
  26. So I fibbed a little.  This issue (#12) of ST PRO GEM started
  27. out to be another discussion of interface issues.  But, as Tolkien
  28. once said, the tale grew in the telling, and this is now the first
  29. of  a series of three articles.   This part will discuss AES event
  30. handling  and  its implications for GEM  program  structure.   The
  31. following  article will contain a "home brew" dialog handler  with
  32. some  new  features,  and  the third will,  finally,  take up  the
  33. discussion  of  interface design,  using the dialog handler as  an
  34. example.   (There is no download for this article.   The downloads
  35. will return, with a vengeance, in ST PRO GEM #13.)
  36.  
  37. .SH ALL FOR ONE, AND ONE FOR ALL.
  38. A quick inspection of the AES
  39. documents  shows that there are five routines devoted  to  waiting
  40. for individual types of events, and one routine, evnt_multi, which
  41. is  used  when more than one type is desired.   This article  will
  42. discuss  ONLY evnt_multi for two reasons.   First,  it is the most
  43. frequently used of the routines.   Second, waiting for one type of
  44. event is a bad practice.   Any event call turns the system over to
  45. the AES and suspends the application and its interaction with  the
  46. user.   In  such  cases,  some  "escape clause",  such as a timer,
  47. should be inserted to revive the program and prompt the user if no
  48. event  is  forthcoming.   Otherwise,  the  application may end  up
  49. apparently  (or  actually)  hung,  with a  resulting  reboot,  and
  50. probably a very annoyed user.
  51. .SH STARTING  AHEAD.
  52. One  possible type of event is a  message.
  53. Messages  are usually sent to the application by the AES,  and are
  54. associated  with  windows or the menu.   Two previous articles  in
  55. this  series have discussed such messages.   ST PRO GEM number two
  56. considered   window  messages,   and  number  seven  handled  menu
  57. messages.  You may want to review these topics before proceeding.
  58. .PP
  59. The actual evnt_multi call is a horrendous thing:
  60. .FB evnt_multi()
  61. ev_which = evnt_multi(ev_flags,
  62.            btn_clicks, btn_mask, btn_state,
  63.            r1_flags, r1_x, r1_y, r1_w, r1_h,
  64.            r2_flags, r2_x, r2_y, r2_w, r2_h,
  65.            &msg_buff,
  66.            time_lo, time_hi,
  67.            &mx, &my, &btn, &kbd, &char, &clicks);
  68. .FE
  69. Each  of  the lines in the call relate to a different  event,  and
  70. they  will be discussed in the order in which they  appear.
  71. .PP
  72. Note  that a call with this number of parameters causes  some
  73. overhead stacking and retrieval of the values.   In  most
  74. cases,  this  should be of little concern on a machine as fast  as
  75. the ST.   However, where throughput is a concern, such as in close
  76. tracking  of the mouse cursor,  you may want to write a customized
  77. binding  for evnt_multi which dispenses with the  parameter  list.
  78. This  can  be accomplished by maintaining the values in  a  static
  79. array  and moving them as a block into the binding  arrays  int_in
  80. (for all values but &msg_buff), and addr_in (for &msg_buff).  Note
  81. that  you  may NOT simply leave the values in  int_in;  other  AES
  82. bindings reuse this space.
  83. .PP
  84. Ev_flags  and ev_which are both 16-bit integers  composed  of
  85. flag bits.  Bits set in ev_flags determine which event(s) the call
  86. will  wait  for;  those  set in ev_which  indicate  what  event(s)
  87. actually occurred.   Both use the following flag bit mnemonics and
  88. functions:
  89. .BO
  90.      0x0001 - MU_KEYBD - Keyboard input
  91. .EO
  92. .BO
  93.      0x0002 - MU_BUTTON - Mouse button(s)
  94. .EO
  95. .BO
  96.      0x0004 - MU_M1 - Mouse rectangle #1
  97. .EO
  98. .BO
  99.      0x0008 - MU_M2 - Mouse rectangle #2
  100. .EO
  101. .BO
  102.      0x0010 - MU_MESAG - AES message
  103. .EO
  104. .BO
  105.      0x0020 - MU_TIMER - Timer
  106. .EO
  107. .sp 1
  108. The  appropriate mnemonics are ORed together to create the  proper
  109. ev_flags value.
  110. .PP
  111. There  is  one  common pitfall here.   Notice  that  multiple
  112. events  may  be reported from one evnt_multi.   Event  merging  is
  113. performed  by the AES in order to save space on the  application's
  114. event queue.   If events have been merged,  more than one bit will
  115. be  set in the ev_which word.   Your application must check ALL of
  116. the  bits  before  returning to a new  evnt_multi  call.   If  you
  117. don't do this, some events may be effectively lost.
  118. .PP
  119. The  first event to be considered is the mouse button.   This
  120. is probably the most difficult event to understand and use, and it
  121. has one major shortcoming.
  122. .PP
  123. The  parameter  btn_clicks tells GEM the  maximum  number  of
  124. clicks which you are interested in seeing.   This value is usually
  125. two,  if your program uses the double-click method, or one if only
  126. single  clicks  are used.   The AES returns the number  of  clicks
  127. which caused the event through &clicks, which must be a pointer to
  128. a word.
  129. .PP
  130. GEM determines the number of clicks by the following  method.
  131. When the first button-down is detected, a time delay is begun.  If
  132. another  complete button-up,  button-down cycle is detected before
  133. the time expires,  then the result is a double click.   Otherwise,
  134. the  event is a single click.    Note that the final state of  the
  135. buttons  is  returned via &btn,  as described below.   By checking
  136. this  final state,  you may determine whether a single click event
  137. ended with the button up (a full click),  or with the button still
  138. down  (which  may  be  interpreted as  the  beginning  of  a  drag
  139. operation).   Double clicking is meaningless,  and not checked, if
  140. the evnt_multi is waiting on more than one button (see below).
  141. .PP
  142. The double-click detection delay is variable,  and may be set
  143. by your program using the call
  144. .FB ev_dclick()
  145. ev_dspeed = ev_dclick(ev_dnew, ev_dfunc);
  146. .FE
  147. Ev_dfunc  is a flag which determines the purpose of the call.   If
  148. it  is  zero,  the  current  double click  speed  is  returned  in
  149. ev_dspeed.   If ev_dfunc is non-zero, then ev_dnew becomes the new
  150. double-click   speed.    Both  ev_dspeed  and  ev_dnew  are  words
  151. containing  a "magic number" between zero and four.   Zero is  the
  152. slowest  (i.e.,  longest)  double-click,  and four is the fastest.
  153. (These  correspond  to  the  slow-fast  range  in  the   Desktop's
  154. Preferences  dialog.)  In general,  you should not reset the click
  155. speed  unless  specifically requested,  because such a change  can
  156. throw off the user's timing and destroy the hand/eye  coordination
  157. involved in using the mouse.
  158. .PP
  159. GEM  was  originally designed to work with  a  single  button
  160. input device.   This allows GEM applications interaction well with
  161. devices such as light pens and digitizing tablets.   However, some
  162. features are available for dealing with multi-button mice like the
  163. ST's.
  164. .PP
  165. The  evnt_multi parameters btn_mask and btn_state  are  words
  166. containing  flag bits corresponding to buttons.   The lowest order
  167. bit corresponds to the left-most button,  and so on.  A bit is set
  168. in  the  btn_mask parameter if the AES is to  watch  a  particular
  169. button.   The  corresponding bit in btn_state is set to the  value
  170. for  which  the program is waiting.   The word returned  via  &btn
  171. uses  the  same  bit system to show the state of  the  buttons  at
  172. completion.   It  is  important to notice that all of  the  target
  173. states in btn_state must occur SIMULTANEOUSLY for the event to  be
  174. triggered.
  175. .PP
  176. Note the limiting nature of this last statement.  It prevents
  177. a  program from waiting for EITHER the left or right button to  be
  178. pressed.  Instead, it must wait for BOTH to be pressed, which is a
  179. difficult  operation  at best.   As a result,  the standard  mouse
  180. button  procedure is practically useless if you want to take  full
  181. advantage  of  both buttons on the ST mouse.   In this case,  your
  182. program  must "poll" the mouse state and  determine  double-clicks
  183. itself.   (More  on  polling later.)  By the way,  many  designers
  184. (myself  included) believe that using both buttons  is  inherently
  185. confusing and should be avoided anyway.
  186. .SH MOUSE RECTANGLES.
  187. One of GEM's nicer features is its ability
  188. to watch the mouse pointer's position for you, and report an event
  189. only  when it enters or departs a given screen region.   Since you
  190. don't  have to track the mouse pixel by pixel,  this eliminates  a
  191. lot  of application over.   The evnt_multi call gives you  the
  192. ability  to  specify one or two rectangular areas  which  will  be
  193. watched.   An event can be generated either when the mouse pointer
  194. enters the rectangle,  or when it leaves the rectangle.  The "r1_"
  195. series  of  parameters specifies one of the  rectangles,  and  the
  196. "r2_" series specifies the other, as follows:
  197. .sp 1
  198. .ti +2
  199. r1_flag, r2_flag - zero if waiting to enter rectangle,
  200. .br
  201. .ti +2
  202.                    one if waiting to leave rectangle
  203. .br
  204. .ti +2
  205. r1_x, r2_x - upper left X raster coordinate of wait rectangle
  206. .br
  207. .ti +2
  208. r1_y, r2_y - upper left Y raster coordinate of wait rectangle
  209. .br
  210. .ti +2
  211. r1_w, r2_w - width of wait rectangle in pixels
  212. .br
  213. .ti +2
  214. r1_h, r2_h - height of wait rectangle in pixels
  215. .br
  216. .sp 1
  217. Each  rectangle  wait will only be active if its  associated  flag
  218. (MU_M1 or MU_M2) was set in ev_flags.
  219. .PP
  220. There  are two common uses of rectangle waits.   The first is
  221. used when creating mouse-sensitive regions on the screen.   Mouse-
  222. sensitive regions, also called "hot spots", are objects which show
  223. a  visual effect,  such as inversion or outlining,  when the mouse
  224. cursor  moves  over them.   The items in a menu dropdown,  or  the
  225. inversion  of  Desktop icons during a drag operation,  are  common
  226. examples.
  227. .PP
  228. Hot spots are commonly created by grouping  the  sensitive
  229. objects  into  one  or  two areas,  and then setting  up  a  mouse
  230. rectangle  wait  for  entering  the  area.    When  the  event  is
  231. generated,  the  &mx  and &my returns may be examined to find  the
  232. true  mouse coordinates,  and objc_find or some other search  will
  233. determine  the affected object.   The object is then  highlighted,
  234. and  a new wait for exiting the object rectangle is  posted.   (ST
  235. PRO  GEM  #13 will show how to create more  complex  effects  with
  236. rectangle waits.)
  237. .PP
  238. The  second common use of rectangle waits is in  animating  a
  239. drag operation.  In many cases, you can use standard AES animation
  240. routines such as graf_dragbox or graf_rubberbox.   In other cases,
  241. you  may  want  a figure other than a simple  box,  or  desire  to
  242. combine   waits  for  other  conditions  such  as  keystrokes   or
  243. collision with hotspots.  Then you will need to implement the drag
  244. operation  yourself,  using  the  mouse rectangles  to  track  the
  245. cursor.
  246. .PP
  247. If you want to track the cursor closely, simply wait for exit
  248. on a one pixel rectangle at the current position,  and perform the
  249. animation routine at each event.  If the drag operation only works
  250. on a grid,  such as character positions,  you can specify a larger
  251. wait  rectangle and only update the display when a legal  boundary
  252. is crossed.
  253. .SH MESSAGES.
  254. The  &msg_buff parameter of evnt_multi gives  the
  255. address  of a 16 byte buffer to receive an AES message.   As noted
  256. above,  I have discussed standard AES messages elsewhere. The last
  257. column  also mentioned that messages may be used to  simulate  co-
  258. routines  within  a single GEM program.
  259. .PP
  260. A  further possibility which bears examination is the use  of
  261. messages  to coordinate the activities of multiple  programs.   In
  262. single-tasking  GEM,  at least one of these programs would have to
  263. be  a  desk accessory.   In any such use of the GEM messages,  you
  264. should pay careful attention to the possibility of overloading the
  265. queue.   Only  eight  slots are provided per task,  and  messages,
  266. unlike events, cannot be merged by the AES.
  267. .SH TIMER.
  268. The  timer event gives you a way of pacing action on
  269. the  screen,  clocking out messages,  or providing a time-out exit
  270. for   an  operation.    Evnt_multi  has  two  16-bit  timer  input
  271. parameters,  time_hi  and  time_lo,  which are the top and  bottom
  272. halves,  respectively,  of  a  32-bit millisecond count.   However,
  273. this documented time resolution must be taken with a grain of salt
  274. on the ST, considering that its internal clock frequency is 200Hz!
  275. .PP
  276. The  timer  event is also extremely useful  for  polling  the
  277. event  queue.   A  "poll"  tests the queue  for  completed  events
  278. without going into a wait state if none are present.  In GEM, this
  279. is   done  by  generating  a  null  event  which   always   occurs
  280. immediately.  A timer count of zero will do just that.
  281. .PP
  282. Therefore,  you  can poll for any set of events by specifying
  283. them  in  the evnt_multi parameters.   A zero timer wait  is  then
  284. added to ensure immediate completion.   Upon return,  if any eveit(s) OTHER than MU_TIMER are set,  a significant event was found
  285. on the queue.  If only MU_TIMER is set, the poll failed to find an
  286. event.
  287. .SH KEYBOARD.
  288. There  are  no input parameters for the  keyboard
  289. event.   The  character  which  is read is returned  as  a  16-bit
  290. quantity through the &char parameter.  For historical reasons, the
  291. codes  which  are returned are compatible with the IBM  PC's  BIOS
  292. level scan codes.  You can find this character table in Appendix D
  293. of  the  GEM VDI manual.   In general,  the high byte need only be
  294. considered  if  the lower byte is zero.   If the low byte is  non-
  295. zero, it is a valid ASCII character.
  296. .PP
  297. Evnt_multi  also returns the status of several modifier  keys
  298. through  the &kbd parameter.   This word contains four significant
  299. bits as follows:
  300. .BO
  301.      0x0001 - Right hand shift key
  302. .EO
  303. .BO
  304.      0x0002 - Left hand shift key
  305. .EO
  306. .BO
  307.      0x0004 - Control key
  308. .EO
  309. .BO
  310.      0x0008 - ALT key
  311. .EO
  312. .sp 1
  313. If  a  bit  is  one,  the key was depressed  when  the  event  was
  314. generated.   Otherwise,  the key was up.  Since the state of these
  315. keys  is already taken into account in generating the  &char  scan
  316. code,  the  &kbd word is most useful when creating enhanced  mouse
  317. functions, such as shift-click or control-drag.
  318. .SH RANDOM NOTES ON EVENTS.
  319. Although the &mx,  &my,  &btn,  and
  320. &kbd returns are nominally associated with particular event types,
  321. they are valid on any return from evnt_multi, and reflect the last
  322. event  which was merged into that return by the AES.  If you  want
  323. more  current values,  you may use graf_mkstate to resample  them.
  324. Whichever method you choose, be consistent within the application,
  325. since  the point of sampling has an effect on mouse  and  keyboard
  326. timing.
  327. .PP
  328. Although  this and preceding columns have been  presented  in
  329. terms of a GEM application,  the event system has many interesting
  330. implications  for desk accessories.   Since the AES scheduler uses
  331. non-preemptive  dispatching,  accessories  have an event  priority
  332. effectively  equal  to  the main  application.   Though  "typical"
  333. accessories  wait  only for AC_OPEN or AC_CLOSE messages  when  in
  334. their  quiescent state,  this is not a requirement of the  system.
  335. Timer  and  other events may also be requested  by  an  accessory.
  336. (Indeed,  there  is  no  absolute requirement  that  an  accessory
  337. advertise  its presence with a menu_register call.)  The  aspiring
  338. GEM hacker might consider how these facts could be used to  create
  339. accessories  similar  to  "BUGS" on the Mac,  or  to  the  "Crabs"
  340. program  described  in  the September,  1985 issue  of  Scientific
  341. American.
  342. .SH EVENTS  AND GEM PROGRAM STRUCTURE.
  343. Although the  evnt_multi
  344. call  might seem to be a small part of the entire GEM system,  its
  345. usage has deep implications for the structure of any  application.
  346. It is generally true that each use of evnt_multi corresponds to  a
  347. mode  in  the  program.   For instance,  form_do contains its  own
  348. evnt_multi,  and its invocation creates a moded dialog.  While the
  349. dialog  is in progress,  other features such as windows and  menus
  350. are unusable.  The graf_dragbox, graf_rubberbox, and graf_slidebox
  351. routines also contain evnt_multi calls.   They create a mode which
  352. is sometimes called "spring-loaded",  since the mode vanishes when
  353. some continuing condition (a depressed mouse button) is removed.
  354. .PP
  355. In consequence,  a well-designed,  non-modal GEM program will
  356. contain only one explicit evnt_multi call.  This call is part of a
  357. top-level  loop  which  decodes events as they  are  received  and
  358. dispatches  control  to  the appropriate  handling  routine.   The
  359. dispatcher  must  always  distinguish  between  event  types.   In
  360. programs  where  multiple windows are used,  it may also  need  to
  361. determine which local data structure is associated with the active
  362. window.
  363. .PP
  364. This  construction  is  sometimes  called  a  "push"  program
  365. structure,  because  it  allows the user to drive the  application
  366. by generating events in any order.  This contrasts with the "pull"
  367. structure of traditional command line or menu programs,  where the
  368. application is in control and demands input at each step before it
  369. proceeds.    "Push"  structure  promotes  consistent  use  of  the
  370. user interface and a feeling of control on the part of the user.
  371. .PP
  372. The  next ST PRO GEM column will look more closely at  events
  373. and  program  structure in the context of a large piece  of  code.
  374. The  code  implements an alternate dialog  handler,  incorporating
  375. mouse-sensitive objects as part of the standard interface.   Since
  376. this  code  is  "open",  it may be modified and  merged  with  any
  377. application's main event loop, resulting in non-modal dialogs.
  378. .!
  379. .!
  380. .!*****************************************************************************
  381. .!*                                          *
  382. .!*                End Part 1                      *
  383. .!*                                          *
  384. .!*****************************************************************************
  385.